﻿using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;
using System.Collections.Generic;
using UnityEngine;

public class TransformVectorUnit : MonoBehaviour {

    [SerializeField]
    TransformVector.Unit ID;

    [SerializeField]
    Material slelectMaterial;

    [SerializeField]
    TransformVector transformVector;

    Material myMaterial;
    MeshRenderer meshRenderer;
    void Awake () {
        meshRenderer = GetComponent<MeshRenderer>();
        myMaterial = meshRenderer.material;
        //transformVector = transform.parent.GetComponent<TransformVector>();
    }
	
	//void Update () {
		
	//}
    Vector3 transformVectorPosition;
    public void Select()
    {
        meshRenderer.material = slelectMaterial;
        if (ID != transformVector.unit && TransformVector.selectUnit)
        {
            TransformVector.selectUnit.NoSelect();
        }
        TransformVector.selectUnit = this;
        transformVector.unit = ID;
        transformVectorPosition = transformVector.transform.position;
    }

    public void NoSelect()
    {
        meshRenderer.material = myMaterial;
        if (TransformVector.selectUnit)
        {
            TransformVector.selectUnit = null;
        }
    }

    public void Move(Vector3 cursorPos)
    {
        //if (transformVector.Look == VoxelEditorMastermind.instance.mainVoxel.transform)
        //{
        //    return;
        //}
        cursorPos = Camera.main.transform.rotation * cursorPos;
        switch (ID)
        {
            case TransformVector.Unit.x:
                transformVector.transform.position = (transformVectorPosition + new Vector3((int)(cursorPos.x * transformVector.transform.localScale.x / 7f), 0, 0));
                transformVector.Look.gameObject.GetComponent<TransformUnitComponent>().SetPositionX(transformVector.transform.position.x);
                break;
            case TransformVector.Unit.y:
                transformVector.transform.position = (transformVectorPosition + new Vector3(0,(int)(cursorPos.y * transformVector.transform.localScale.y / 7f), 0));
                transformVector.Look.gameObject.GetComponent<TransformUnitComponent>().SetPositionY(transformVector.transform.position.y);
                break;
            case TransformVector.Unit.z:
                transformVector.transform.position = (transformVectorPosition + new Vector3(0, 0, (int)(cursorPos.z * transformVector.transform.localScale.z / 7f)));
                transformVector.Look.gameObject.GetComponent<TransformUnitComponent>().SetPositionZ(transformVector.transform.position.z);
                break;
            case TransformVector.Unit.no:
                break;
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        
    }
}
